package com.yanqu.road.server.cmd.yrank.user;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.rank.yrank.UserYRank;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.commonactivity.CommonActivityProto;
import com.yanqu.road.pb.orewaractivity.OreWarActivityProto;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.pb.rank.YRankProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityInfoMgr;
import com.yanqu.road.server.manager.activity.biaoju.CrossBiaoJuActivity;
import com.yanqu.road.server.manager.activity.biaoju.CrossBiaoJuMgr;
import com.yanqu.road.server.manager.yrank.CrossYRankCacheMgr;
import com.yanqu.road.server.manager.yrank.CrossYRankMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.List;

@Cmd(code = CrossProtocol.C2_CROSS_YRANK_GET_USER_RANK_LIST, desc = "")
public class CrossYRankGetUserRankListCmd extends CrossUnionCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        YRankProto.YRankReqMsg reqMsg = YRankProto.YRankReqMsg.parseFrom(packet.getBytes());
        int conditionType = Integer.valueOf(reqMsg.getIdKey());
        if (conditionType == eGamePlayerEventType.BiaoJuExpRank.getValue()) {
            CrossBiaoJuActivity activity = CrossBiaoJuMgr.getActivity(reqMsg.getActivityId(), reqMsg.getGroupId());
            if (activity != null) {
                activity.sendUserRankList(packet.getPlayerId(), serverId);
            }
            return;
        }

        RankProto.RankListMsg.Builder rankBuilder = CrossYRankCacheMgr.getUserRankList(reqMsg.getActivityId(), reqMsg.getGroupId(), reqMsg.getIdKey());
        int rankNum = CrossYRankCacheMgr.getUserRank(reqMsg.getActivityId(), reqMsg.getGroupId(), reqMsg.getIdKey(), rankBuilder, packet.getPlayerId());

//        List<UserYRank> rankList = CrossYRankMgr.getSortUserRankList(reqMsg.getActivityId(), reqMsg.getGroupId(), reqMsg.getIdKey());
//        int rankNum = CrossYRankMgr.getUserRankNum(rankList, packet.getPlayerId());
//
//        RankProto.RankListMsg.Builder rankBuilder = RankProto.RankListMsg.newBuilder();
//        for (UserYRank rank : rankList) {
//            rankBuilder.addRankList(RankProto.RankTempMsg.newBuilder()
//                    .setUserId(rank.getUserId())
//                    .setParam(rank.getParam())
//                    .setValue(rank.getValue().toString())
//                    .setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(rank.getUserBaseInfo()))
//            );
//        }

        ActivityInfo activityInfo = ActivityInfoMgr.getActivityInfo(activityId);
        int actvityType = 0;
        if(null != activityInfo){
            actvityType = activityInfo.getType();
        }
        if (actvityType == eActivityType.OreWarActivity.getValue()){
            OreWarActivityProto.OreWarActivityRankListRespMsg.Builder respMsg = OreWarActivityProto.OreWarActivityRankListRespMsg.newBuilder();
            respMsg.setMyRank(rankNum);
            respMsg.setRankList(rankBuilder);
            YanQuMessage message = YanQuMessageUtils.buildMessage(Protocol.U_ORE_WAR_ACTIVITY_RANK, respMsg);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), message);
            return;
        }


        CommonActivityProto.CommonActivityRankListRespMsg.Builder builder = CommonActivityProto.CommonActivityRankListRespMsg.newBuilder();
        builder.setMyRank(rankNum);
        builder.setRankList(rankBuilder);
        if (actvityType == eActivityType.CrossTradeWarActivity.getValue()){
            YanQuMessage message = YanQuMessageUtils.buildMessage(Protocol.U_TRADE_WAR_ACTIVITY_RANK, builder);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), message);
            return;
        }
        YanQuMessage message = YanQuMessageUtils.buildMessage(Protocol.U_ACTIVITY_RANK_LIST, builder);
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), message);
    }

    @Override
    public void sendData() {

    }
}
